﻿Imports System.Xml.Serialization

Public Class Manager
    Private Sub New()
    End Sub

#Region " Events "
    Public Shared Event LogEvent(ByVal text As String, ByVal tag As Object)
    Friend Shared Sub OnLogEvent(ByVal text As String, Optional ByVal tag As Object = Nothing)
        Utils.Threading.Invoke(AddressOf OnLogEvent_Callback, text, tag)
    End Sub
    Private Shared Sub OnLogEvent_Callback(ByVal text As String, ByVal tag As Object)
        RaiseEvent LogEvent(text, tag)
    End Sub

    Public Shared Event SharedItemCreated(ByVal item As Entities.SharedItem)
    Friend Shared Sub OnSharedItemCreated(ByVal item As Entities.SharedItem)
        Utils.Threading.Invoke(AddressOf OnSharedItemCreated_Callback, item)
    End Sub
    Private Shared Sub OnSharedItemCreated_Callback(ByVal item As Entities.SharedItem)
        RaiseEvent SharedItemCreated(item)
    End Sub

    Public Shared Event SharedItemDeleted(ByVal item As Entities.SharedItem)
    Friend Shared Sub OnSharedItemDeleted(ByVal item As Entities.SharedItem)
        Utils.Threading.Invoke(AddressOf OnSharedItemDeleted_Callback, item)
    End Sub
    Private Shared Sub OnSharedItemDeleted_Callback(ByVal item As Entities.SharedItem)
        RaiseEvent SharedItemDeleted(item)
    End Sub

    Public Shared Event SharedItemUpdated(ByVal item As Entities.SharedItem)
    Friend Shared Sub OnSharedItemUpdated(ByVal item As Entities.SharedItem)
        Utils.Threading.Invoke(AddressOf OnSharedItemUpdated_Callback, item)
    End Sub
    Private Shared Sub OnSharedItemUpdated_Callback(ByVal item As Entities.SharedItem)
        RaiseEvent SharedItemUpdated(item)
    End Sub

    Public Shared Event ShareDeleted(ByVal share As Entities.SharedWatchFolder)
    Friend Shared Sub OnShareDeleted(ByVal share As Entities.SharedWatchFolder)
        Utils.Threading.Invoke(AddressOf OnShareDeleted_Callback, share)
    End Sub
    Private Shared Sub OnShareDeleted_Callback(ByVal share As Entities.SharedWatchFolder)
        RaiseEvent ShareDeleted(share)
    End Sub




#End Region

    Private Shared mMessageSubscriber As New MessageSubscriber

    Public Shared Sub Start()
        RescanWorker.Start()

        NetNode.Manager.RegisterMessageSubscriber(mMessageSubscriber)
    End Sub
    Public Shared Sub [Stop]()
        FViewSharedItems.CloseAll()
        FViewSharedItemDetails.CloseAll()
        RescanWorker.Stop()
    End Sub

    Public Shared Sub RescanShare(ByVal share As Entities.SharedWatchFolder)
        RescanQueue.Queue(share, share.Path, TimeSpan.FromSeconds(0))
    End Sub

    Friend Shared ReadOnly TargetName As String = "FileShare"

    Private Class MessageSubscriber
        Implements NetNode.IMessageSubscriber

        Public ReadOnly Property Code() As String Implements NetNode.IMessageSubscriber.Code
            Get
                Return Manager.TargetName
            End Get
        End Property

        Public Sub IncomingMessage(ByVal client As NetNode.Client, ByVal message As NetNode.MessageBlock) Implements NetNode.IMessageSubscriber.IncomingMessage
            Manager.OnIncomingMessage(client, message)
        End Sub
    End Class

    Private Shared Sub OnIncomingMessage(ByVal client As NetNode.Client, ByVal message As NetNode.MessageBlock)
        Select Case message.Message
            Case "GetShares"
                Commands.GetShares.HandleRequest(client, message)
            Case "GetSharesReply"
                Commands.GetShares.HandleReply(client, message)
            Case Commands.GetShareItems.RequestCode
                Commands.GetShareItems.HandleRequest(client, message)
            Case Commands.GetShareItems.ReplyCode
                Commands.GetShareItems.HandleReply(client, message)

            Case Commands.GetSharedItemDetails.RequestCode
                Commands.GetSharedItemDetails.HandleRequest(client, message)
            Case Commands.GetSharedItemDetails.ReplyCode
                Commands.GetSharedItemDetails.HandleReply(client, message)

            Case Else
                Manager.OnLogEvent(String.Format("Unknown message: {0}", message.Message))
        End Select
    End Sub

    Public Shared Sub DeleteShare(ByVal share As Entities.SharedWatchFolder)
        DataStore.SharedWatchFolders.Delete(share.Id)
        OnShareDeleted(share)
    End Sub

    Public Shared Function StartGetShares(ByVal client As NetNode.Client) As Integer
        Return Commands.GetShares.SendRequest(client)
    End Function
    Public Shared Function StartGetShareItems(ByVal client As NetNode.Client, ByVal share As Entities.SharedWatchFolder, ByVal directoryCode As String) As Integer
        Return Commands.GetShareItems.SendRequest(client, share, directoryCode)
    End Function
    Public Shared Function StartGetSharedItemDetails(ByVal client As NetNode.Client, ByVal sharedItemId As Integer) As Integer
        Return Commands.GetSharedItemDetails.SendRequest(client, sharedItemId)
    End Function

    Public Shared Event GetSharesReply(ByVal client As NetNode.Client, ByVal reply As Commands.GetShares.Reply)
    Friend Shared Sub OnGetSharesReply(ByVal client As NetNode.Client, ByVal reply As Commands.GetShares.Reply)
        RaiseEvent GetSharesReply(client, reply)
    End Sub

    Public Shared Event GetShareItemsReply(ByVal client As NetNode.Client, ByVal reply As Commands.GetShareItems.Reply)
    Friend Shared Sub OnGetShareItemsReply(ByVal client As NetNode.Client, ByVal reply As Commands.GetShareItems.Reply)
        RaiseEvent GetShareItemsReply(client, reply)
    End Sub

    Public Shared Event GetSharedItemDetailsReply(ByVal client As NetNode.Client, ByVal reply As Commands.GetSharedItemDetails.Reply)
    Friend Shared Sub OnGetSharedItemDetailsReply(ByVal client As NetNode.Client, ByVal reply As Commands.GetSharedItemDetails.Reply)
        RaiseEvent GetSharedItemDetailsReply(client, reply)
    End Sub
End Class


